package com.example.conditional;

import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
import java.util.Arrays;

public class ProfileMatchCondition implements Condition {
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        String[] requiredProfiles = (String[]) metadata.getAnnotationAttributes(ProfileConditional.class.getName()).get("value");
        String activeProfile = context.getEnvironment().getProperty("spring.profiles.active", "");
        return Arrays.stream(requiredProfiles).anyMatch(p -> p.equalsIgnoreCase(activeProfile));
    }
}